package com.alibaba.citrus.ots.application.report.ability.repository.impl;

import com.alibaba.citrus.ots.application.report.ability.repository.MonthSalesEmployeeRankReportQueryRepository;
import com.alibaba.cz.base.tool.page.PageResult;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.otsapplicationreportsdk.model.dto.MonthSalesBasicReportSDO;
import com.epoch.app.otsapplicationreportsdk.model.dto.MonthSalesEmployeeRankReportLoadListRequest;
import com.epoch.app.otsapplicationreportsdk.model.dto.MonthSalesEmployeeRankReportLoadListVO;
import com.epoch.app.otsapplicationreportsdk.model.dto.QueryMonthSalesReportRequest;
import com.epoch.app.otsapplicationreportsdk.monthsalesemployeerankreport.model.MonthSalesEmployeeRankReport;
import com.epoch.app.otsapplicationreportsdk.monthsalesemployeerankreport.service.MonthSalesEmployeeRankReportService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhoubc
 * @description: TODO
 * @date 2021/12/2 19:32
 */
@Component
public class MonthSalesEmployeeRankReportQueryRepositoryImpl implements MonthSalesEmployeeRankReportQueryRepository {

    @Resource
    MonthSalesEmployeeRankReportService monthSalesEmployeeRankReportService;

    @Override
    public PageResult<List<MonthSalesBasicReportSDO>> pageQueryList(QueryMonthSalesReportRequest request) {
        MonthSalesEmployeeRankReportLoadListVO monthSalesEmployeeRankReportLoadListVO = loadListRequestConverter(request);
        Result<List<MonthSalesEmployeeRankReport>> list = monthSalesEmployeeRankReportService.loadListForPage(monthSalesEmployeeRankReportLoadListVO);
        List<MonthSalesBasicReportSDO> result = list.getResult().stream().map(x -> {
            MonthSalesBasicReportSDO bean = new MonthSalesBasicReportSDO();
            BeanUtils.copyProperties(x,bean);
            return bean;
        }).collect(Collectors.toList());
        return PageResult.of(result,list.getTotal());
    }

    @Override
    public PageResult<List<MonthSalesBasicReportSDO>> pageQueryMonthSalesEmployeeBankReportList(QueryMonthSalesReportRequest request) {
        MonthSalesEmployeeRankReportLoadListVO monthSalesEmployeeRankReportLoadListVO = loadListRequestConverter(request);
        Result<List<MonthSalesEmployeeRankReport>> list = monthSalesEmployeeRankReportService.loadListForPageSort(monthSalesEmployeeRankReportLoadListVO);
        List<MonthSalesBasicReportSDO> result = list.getResult().stream().map(x -> {
            MonthSalesBasicReportSDO bean = new MonthSalesBasicReportSDO();
            BeanUtils.copyProperties(x,bean);
            bean.setId(String.valueOf(x.getId()));
            return bean;
        }).collect(Collectors.toList());
        return PageResult.of(result,list.getTotal());
    }

    @Override
    public List<MonthSalesBasicReportSDO> getEmployeeBankReportList(QueryMonthSalesReportRequest request) {
        MonthSalesEmployeeRankReportLoadListRequest queryRequest = getListRequestConverter(request);
        Result<List<MonthSalesEmployeeRankReport>> list = monthSalesEmployeeRankReportService.loadList(queryRequest);
        List<MonthSalesBasicReportSDO> result = list.getResult().stream().map(x -> {
            MonthSalesBasicReportSDO bean = new MonthSalesBasicReportSDO();
            BeanUtils.copyProperties(x,bean);
            return bean;
        }).collect(Collectors.toList());
        return result;
    }

    @Override
    public List<MonthSalesBasicReportSDO> statisticalQuery(QueryMonthSalesReportRequest request) {
        MonthSalesEmployeeRankReportLoadListVO monthSalesEmployeeRankReportLoadListVO = loadListRequestConverter(request);
        Result<List<MonthSalesEmployeeRankReport>> response = monthSalesEmployeeRankReportService.statisticalQuery(monthSalesEmployeeRankReportLoadListVO);
        List<MonthSalesBasicReportSDO> result = response.getResult().stream().map(x -> {
            MonthSalesBasicReportSDO bean = new MonthSalesBasicReportSDO();
            BeanUtils.copyProperties(x,bean);
            return bean;
        }).collect(Collectors.toList());
        return result;
    }

    private MonthSalesEmployeeRankReportLoadListVO loadListRequestConverter(QueryMonthSalesReportRequest request){
        MonthSalesEmployeeRankReportLoadListVO listVO = MonthSalesEmployeeRankReportLoadListVO.newMonthSalesEmployeeRankReportLoadListVOBuilder()
                .orgBusinessUnitIds(request.getOrgBusinessUnitIds())
                .orgSalesOrganizationIds(request.getOrgSalesOrganizationIds())
                .orgSalesChannelIds(request.getOrgSalesChannelIds())
                .orgSalesDepartmentIds(request.getOrgSalesDepartmentIds())
                .channelManageEmployeeIds(request.getChannelManageEmployeeIds())
                .departmentSimpleName(request.getDepartmentSimpleName())
                .departmentManageEmployeeIds(request.getDepartmentManageEmployeeIds())
                .manageEmployeeIds(request.getManageEmployeeIds())
                .statisticalDate(request.getStatisticalDate())
                .dataMark(request.getDataMark())
                .build();
        listVO.setDir(request.getDir());
        listVO.setSortType(request.getSortType());
        listVO.setStart(request.getStart());
        listVO.setLimit(request.getLimit());
        return listVO;
    }

    private MonthSalesEmployeeRankReportLoadListRequest getListRequestConverter(QueryMonthSalesReportRequest request){
        MonthSalesEmployeeRankReportLoadListRequest listRequest = MonthSalesEmployeeRankReportLoadListRequest.newMonthSalesEmployeeRankReportLoadListRequestBuilder()
                .orgBusinessUnitId(request.getOrgBusinessUnitId())
                .orgSalesOrganizationId(request.getOrgSalesOrganizationId())
                .orgSalesChannelId(request.getOrgSalesChannelId())
                .orgSalesDepartmentId(request.getOrgSalesDepartmentId())
                .manageEmployeeId(request.getManageEmployeeId())
                .planTime(request.getPlanMonth())
                .statisticalDate(request.getStatisticalDate())
                .build();
        return listRequest;
    }
}
